package com.teacold.customdemo.chapter_six;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BaseLineDemo extends View {
    public BaseLineDemo(Context context) {
        super(context);
    }

    public BaseLineDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseLineDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        getBaseLine(canvas);
    }

    /**
     * 文字绘制的矩形左上角点
     * 获取文字的baseline
    * */
    private void getBaseLine(Canvas canvas){
        String text="harvic\'s blog";
        int top=200;
        int baseLineX=0;
        Paint paint=new Paint();
        paint.setTextSize(120);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX,top,3000,top,paint);
        //计算出baseLine线的位置
        Paint.FontMetricsInt fontMetricsInt=paint.getFontMetricsInt();
        int baseLineY=top-fontMetricsInt.top;
        Log.e("ssssss","baseLineY"+baseLineY);
        //画基线
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX,baseLineY,3000,baseLineY,paint);
        paint.setColor(Color.BLUE);
        canvas.drawText(text,baseLineX,baseLineY,paint);

    }
    /**
     * 关于paint的fontMetrics
     * ascent 字符应当的最高高度所在县
     * descent 字符应当的最低高度所在线
     * top 可绘制的最高高度所在线
     * bottom 可绘制的最低低度所在线
     */
    private void fontMetrics(Canvas canvas) {
        int baseLineX = 0;
        int baseLineY = 200;
        Paint paint = new Paint();
        paint.setTextSize(120);
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText("harvic\'s blog", baseLineX, baseLineY, paint);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float ascent = baseLineY + fontMetrics.ascent;
        float descent = baseLineY + fontMetrics.descent;
        float top = baseLineY + fontMetrics.top;
        float bottom = baseLineY + fontMetrics.bottom;
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX, top, 3000, top, paint);
        paint.setColor(Color.GREEN);
        canvas.drawLine(baseLineX, ascent, 3000, ascent, paint);
        paint.setColor(Color.BLUE);
        canvas.drawLine(baseLineX, descent, 3000, descent, paint);
        paint.setColor(Color.YELLOW);
        canvas.drawLine(baseLineX, bottom, 3000, bottom, paint);
        Log.e("ssssssssss","bottom"+bottom+"      descent"+descent);
    }

    /**
     * 基本的绘制text
     */
    private void drawText(Canvas canvas) {
        int baseLineX = 0;
        int baseLineY = 200;
        //画基线
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX, baseLineY, 3000, baseLineY, paint);
        //写文字
        paint.setColor(Color.GREEN);
        paint.setTextSize(120);
//        paint.setTextAlign(Paint.Align.CENTER);字体从什么位置开始绘制
        canvas.drawText("harvic\'s blog", baseLineX, baseLineY, paint);
    }
}
